from model.team import TeamSpaceModel
from util.db import Cursor
from flask import session


class BookSpaceModel:

    def __init__(self, book_id):
        self.book_id = book_id

    @staticmethod
    def get_user_book_space():
        with Cursor() as cursor:
            user_id = session.get('user_id')
            sql = """select * from yugong_book where team_id in (select team_id from yugong_role_team where joind=1 and user_id=%s) and user_id != %s and auth != 1
                     union all
                     select * from yugong_book where user_id=%s
            """
            cursor.execute(sql, [user_id, user_id, user_id])
            data = cursor.fetchall()
        return data

    @property
    def is_public(self) -> bool:
        with Cursor() as cursor:
            sql = """select auth from yugong_book where id=%s"""
            cursor.execute(sql, [self.book_id])
            data = cursor.fetchall()
            if data[0]['auth'] == 0:
                return True
            return False

    @property
    def is_private(self) -> bool:
        with Cursor() as cursor:
            sql = """select auth from yugong_book where id=%s"""
            cursor.execute(sql, [self.book_id])
            data = cursor.fetchall()
            if data[0]['auth'] == 1:
                return True
            return False

    @property
    def is_protected(self) -> bool:
        with Cursor() as cursor:
            sql = """select auth from yugong_book where id=%s"""
            cursor.execute(sql, [self.book_id])
            data = cursor.fetchall()
            if data[0]['auth'] == 2:
                return True
            return False

    @staticmethod
    def get_book_space_without_login(user_id):
        # 未登录 返回公开个人创建的知识库或者加入的公开的
        with Cursor() as cursor:
            sql = """select * from `yugong_book` where auth=0 and user_id=%s
                     union  all
                     select * from `yugong_book` where auth=0 and team_id in (select team_id from yugong_role_team where user_id=%s and joind=1)
            """
            cursor.execute(sql, [user_id, user_id])
            book_space_list = cursor.fetchall()
        return list(book_space_list)

    @staticmethod
    def get_book_space_with_login(team_id=None):
        user_id = session.get('user_id')
        result = []
        with Cursor() as cursor:
            if team_id:
                sql = """select * from (select * from `yugong_book` where `team_id`=%s and user_id=%s
                         union all
                         select * from `yugong_book` where `team_id`=%s and user_id!=%s and auth!=1) as a
                         LEFT JOIN 
                        (SELECT username, id FROM yugong_user) as b
                        ON a.user_id=b.id
                        """
                cursor.execute(sql, [team_id, user_id, team_id, user_id])
            else:
                # 首先获取所有加入的团队空间
                sql = """select * from (select * from `yugong_book` where user_id=%s and `team_id` in (select team_id from yugong_role_team WHERE user_id=%s)
                         union all
                         select * from `yugong_book` where user_id!=%s and auth!=1 and `team_id` in (select team_id from yugong_role_team WHERE user_id=%s)) as a
                         LEFT JOIN 
                        (SELECT username, id FROM yugong_user) as b
                        ON a.user_id=b.id
                        """
                cursor.execute(sql, [user_id, user_id, user_id, user_id])
            book_space_list = cursor.fetchall()
            role = TeamSpaceModel.get_team_role(id=team_id, id_type='team')
            for item in book_space_list:
                item['create_time'] = item['create_time'].strftime('%Y-%m-%d %H:%M:%S')
                if item['auth'] == 0:
                    item['auth_cn'] = '公开可见'
                if item['auth'] == 1:
                    item['auth_cn'] = '私人可见'
                if item['auth'] == 2:
                    item['auth_cn'] = '空间成员可见'
                if item['auth'] == 0 or role:
                    result.append(item)
        return result
